package com.cw.jeeyt.controller.system;

import java.util.ArrayList;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cw.jeeyt.common.DataTable;
import com.cw.jeeyt.common.ResultData;
import com.cw.jeeyt.common.enums.EnumOperateFlag;
import com.cw.jeeyt.common.web.BaseController;

import com.cw.jeeyt.service.bo.system.SysScaffoldConfigBo;
import com.cw.jeeyt.api.dto.response.JsonResultDto;
import com.cw.jeeyt.service.intf.system.SysScaffoldConfigService;
import com.cw.jeeyt.service.directive.PageDictDirective;
import com.cw.jeeyt.service.directive.PermissionDirective;
import com.cw.lang.common.utils.CollectionUtil;
import com.cw.lang.common.utils.PageUtil;
import com.cw.lang.mybatis.dto.PageInfo;
import com.cw.lang.mybatis.dto.PageParam;
import lombok.extern.slf4j.Slf4j;
/**
 * 系统脚手架配置控制层
 *
 * @author chenw
 * @date 2020-05-24 21:04:55
 */
@Controller
@RequestMapping( "/sysScaffoldConfig" )
@Slf4j
public class SysScaffoldConfigController extends BaseController{
	@Resource
	private SysScaffoldConfigService sysScaffoldConfigService;
	/**
	 * 跳转到系统脚手架配置列表查询页面
	 *
	 * @return
	 */
	@RequestMapping( value = "/query" )
	public ModelAndView query() {
		
		ModelAndView mv = new ModelAndView("system/sysScaffoldConfig_index");
		mv.addObject("permission", new PermissionDirective());
		mv.addObject("PageDict", new PageDictDirective());
		return mv;
	}
	/**
	 * 加载DataTable列表数据
	 *
	 * @param dataTable datatable数据
	 * @return
	 */
	@ResponseBody
	@RequestMapping( value = "/listSysScaffoldConfig")
	public ResultData listSysScaffoldConfig(@RequestBody DataTable dataTable){
		
		SysScaffoldConfigBo sysScaffoldConfigBo = JSONObject.parseObject(JSON.toJSONString(dataTable.queryCond),SysScaffoldConfigBo.class); 
		if(sysScaffoldConfigBo == null){
			sysScaffoldConfigBo = new SysScaffoldConfigBo();
		}

		PageParam pageParam = new PageParam();
        pageParam.setPageNum(PageUtil.calPageNum(dataTable.getStart(),dataTable.getLength()));
        pageParam.setPageSize(dataTable.getLength());
		PageInfo<SysScaffoldConfigBo> pageInfo = sysScaffoldConfigService.selectPage(sysScaffoldConfigBo,pageParam);
		return ResultData.builder()
            .draw(dataTable.getDraw())
            .data(CollectionUtil.isNotEmpty(pageInfo.getList())?pageInfo.getList():new ArrayList())
            .recordsTotal(pageInfo.getTotal())
            .recordsFiltered(pageInfo.getTotal())
            .build();
	}
	
	/**
	 * 跳转到系统脚手架配置表单页面
	 *
	 * @param operation 操作  add 新增 edit修改 view查看
	 * @return
	 * @throws Exception
	 */
	@RequestMapping( value = "/to-form" )
	public ModelAndView editEdit(Long id,String operation) {
		
		ModelAndView mv = new ModelAndView("system/sysScaffoldConfig_form");
		SysScaffoldConfigBo sysScaffoldConfigBo = null;
		if(EnumOperateFlag.ADD.getCode().equals(operation)){
			sysScaffoldConfigBo = new SysScaffoldConfigBo();
		}else{
			sysScaffoldConfigBo = sysScaffoldConfigService.selectByKey(id);
		}
		mv.addObject("sysScaffoldConfig", sysScaffoldConfigBo);
		mv.addObject("operation", operation);
		mv.addObject("PageDict", new PageDictDirective());
		mv.addObject("permission", new PermissionDirective());
		return mv;
	}
	/**
	 * 根据操作类型保存系统脚手架配置数据
	 *
	 * @param sysScaffoldConfigBo
	 * @param operation add 新增  edit 修改
	 */
	@ResponseBody
	@RequestMapping(value = "/saveSysScaffoldConfig",method=RequestMethod.POST)
	public JsonResultDto saveSysScaffoldConfig(SysScaffoldConfigBo sysScaffoldConfigBo,String operation) {
	    
		JsonResultDto jsonResultDto = new JsonResultDto(false);
		try{
			if(operation.equals(EnumOperateFlag.ADD.getCode())){
				sysScaffoldConfigService.addSysScaffoldConfig(sysScaffoldConfigBo);
			}else if(operation.equals(EnumOperateFlag.EDIT.getCode())){
				sysScaffoldConfigService.modifySysScaffoldConfig(sysScaffoldConfigBo);
			}
			jsonResultDto.setMsg("保存成功");
            jsonResultDto.setSuccess(true);
		}catch(Exception e){
			e.printStackTrace();
			jsonResultDto.setMsg("保存失败:" + e.getMessage());
		}
		return jsonResultDto;
	}	
	/**
	 * 删除系统脚手架配置
	 *
	 * @param sysScaffoldConfigBo
	 * @throws Exception
	 */ 
	@RequestMapping( value = "/delete" )
	@ResponseBody
	public JsonResultDto delete(SysScaffoldConfigBo sysScaffoldConfigBo) throws Exception{
		
		JsonResultDto jsonResultDto = new JsonResultDto(false);
		try{
			sysScaffoldConfigService.deleteSysScaffoldConfig(sysScaffoldConfigBo);
			jsonResultDto.setMsg("删除成功");
            jsonResultDto.setSuccess(true);
		}catch(Exception e){
			e.printStackTrace();
			jsonResultDto.setMsg("删除失败:" + e.getMessage());
		}
		return jsonResultDto;
	}

    /**
     * 根据配置生成系统脚手架
     *
     * @param sysScaffoldConfigBo
     * @throws Exception
     */
    @RequestMapping( value = "/generateScaffold" )
    @ResponseBody
    public JsonResultDto generateScaffold(SysScaffoldConfigBo sysScaffoldConfigBo) throws Exception{

        JsonResultDto jsonResultDto = new JsonResultDto(false);
        try{
            sysScaffoldConfigService.generateScaffold(sysScaffoldConfigBo);
            jsonResultDto.setMsg("脚手架生成成功");
            jsonResultDto.setSuccess(true);
        }catch(Exception e){
            e.printStackTrace();
            jsonResultDto.setMsg("脚手架生成失败:" + e.getMessage());
        }
        return jsonResultDto;
    }
}
